package com.glory.brepda.utils;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.glory.brepda.R;
import com.glory.brepda.activity.mfg.CommonlyBeginActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.AvailableProgramBean;
import com.glory.brepda.entity.BinInfoEntity;
import com.glory.brepda.entity.EDCListBean;
import com.glory.brepda.entity.EqpInfoEntity;
import com.glory.brepda.entity.EquipMentEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.LotList;
import com.glory.brepda.entity.LotLists;
import com.glory.brepda.entity.LotactionList;
import com.glory.brepda.entity.LotactionLists;
import com.glory.brepda.entity.PackageDetailListEntity;
import com.glory.brepda.entity.PackageEntity;
import com.glory.brepda.entity.RecipeInfoEntity;
import com.glory.brepda.entity.ScrapEntity;
import com.glory.brepda.entity.ToolEntity;
import com.glory.brepda.entity.UpdateIPEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.FragmentManager;

/**
 * FileName: LotUtils
 * Author: XJH
 * Date: 2020/5/26 16:38
 * Description:常规操作
 * History:
 */
public class LotUtils {

    public static void searchLotInfo(Context context, String lotID, FragmentManager fragmentManager, netCallback listener) {
        FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                ("正在查询LOT信息", new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                }.getType(), fragmentManager, context) {

            @Override
            public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(context, true, fragmentManager)) {
                    if (listener != null) {
                        listener.onSuccess(response.body().getResponse().getBody());
                    }
                }

            }

            @Override
            public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                super.onError(response);
                if (listener != null) {
                    listener.onFail(response.getException());
                }
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "LOTINFOBYID");
        map.addProperty("LOTID", lotID);
        map.addProperty("LOTALIAS", "");
        map.addProperty("DURABLE", "");
        map.addProperty("EQUIPMENTID", "");
        map.addProperty("STEPNAME", "");
        NetUtils.requestPostNet("IF.LOTQUERY", "Common", context, map, callBack);
//        NetUtils.requestMESPostNet("CPC.LOTQUERY",context,map,callBack);
    }

    public static void searchEqpInfo(String eqpID, Context context, FragmentManager fragmentManager, netCallback listener) {
        FreeUI_EntityCallBack<BaseResponseBean<EqpInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<EqpInfoEntity>>
                ("正在查询设备信息", new TypeToken<BaseResponseBean<EqpInfoEntity>>() {
                }.getType(), fragmentManager, context) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EqpInfoEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(context, true, fragmentManager)) {
                    if (listener != null) {
                        listener.onSuccess(response.body().getResponse().getBody());
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EqpInfoEntity>> response) {
                super.onError(response);
                if (listener != null) {
                    listener.onFail(response.getException());
                }
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "LOAD");
        EquipMentEntity equipMentEntity = new EquipMentEntity();
        equipMentEntity.setEQUIPMENTID(eqpID);
        map.add("EQUIPMENT", new JsonParser().parse(new Gson().toJson(equipMentEntity)).getAsJsonObject());
        NetUtils.requestPostNet("IF.BREQUERYEQP", "Common", context, map, callBack);
    }

    public static void lotTrackIn(FragmentManager fragmentManager,LotInfoEntity lotInfoEntity,List<String> durableidList,String woid,String method, Activity context, String eqpID, String lotID, List<String> carrierList, String actionType) {
        FreeApi_DialogUtils.getConfirmAndCancelDialog(context, "确定要提交?", new FreeUI_CommonCallback() {
            @Override
            public void onSuccess() {
                //确定

                FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                        (new TypeToken<BaseResponseBean<JSONObject>>() {
                        }.getType(), fragmentManager, context) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(context, false, fragmentManager)) {

                            FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack2 = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                                    (new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                                    }.getType(), fragmentManager, this) {

                                @Override
                                public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                                    super.onSuccess(response);
                                }
                                @Override
                                public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                                    super.onError(response);
                                }
                            };
                            NetUtils.canceleqpidle(eqpID, new JsonArray(), context, callBack2);

                            FreeApi_ToastUtils.showFreeToast("进站成功", context, true, 0);
                            context.finish();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<JSONObject>> response) {
                        super.onError(response);
                        FreeApi_LogUtils.saveErrorMessages(response.getException(), "LOTTRACKIN");
                        FreeApi_ToastUtils.showFreeToast("连接服务器失败", context, false, 0);
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE", actionType);
                map.addProperty("EQUIPMENTID", eqpID);
                if (method.equals("MULTIDEVICELOTTRACKIN")){
                    map.addProperty("TYPE", "1");
                }
                map.addProperty("ISCHECKSTEPAUTHORITY", true);
                map.addProperty("BDCODE", Constant.BDCODE);
                map.addProperty("LIMITPACKAGETYPEQTY", Constant.LIMITPACKAGETYPEQTY);
                map.addProperty("PACKAGETYPE", Constant.PACKAGETYPE);
                map.addProperty("PARTNAME", Constant.PARTNAME);
                map.addProperty("PARTTYPE", lotInfoEntity.getLOT().getPARTTYPE());
                map.addProperty("CUSTOMERCODE",lotInfoEntity.getLOT().getCUSTOMERCODE());
                map.addProperty("SPC2", lotInfoEntity.getPART().getSPEC2());
                map.addProperty("WIRESPEC", Constant.WIRESPEC);
                map.addProperty("WOID", woid);
//                map.addProperty("WORKORDER", );
                map.addProperty("ORDER_NO", lotInfoEntity.getLOT().getWOID());
                map.addProperty("EXPIRE_TIME", lotInfoEntity.getFUTURETIMERINSTANCELIST().size() > 0?lotInfoEntity.getFUTURETIMERINSTANCELIST().get(0).getEXPIRE_TIME():"");
//                map.add("DURABLELIST", new JsonParser().parse(new Gson().toJson(carrierList)).getAsJsonArray());
//                List<LotList> lotList = new ArrayList<>();
//                lotList.add(new LotList(lotID));
                List<LotLists> lotLists = new ArrayList<>();
                List<LotactionList> LotactionList = new ArrayList<>();
                lotLists.add(new LotLists(lotID));
                LotactionList.add(new LotactionList(lotID, "", "", "", "", ""));
                map.add("LOTLIST", new JsonParser().parse(new Gson().toJson(lotLists)).getAsJsonArray());//传批次集合
                map.add("LOTACTIONLIST", new JsonParser().parse(new Gson().toJson(LotactionList)).getAsJsonArray());//传批次集合
                if (durableidList != null) {
                    map.add("DURABLEIDLIST", new JsonParser().parse(new Gson().toJson(durableidList)).getAsJsonArray());//传批次集合
                }

                // 25/4/25 901713 粘片批次进站新增框架扫描进站
                if (StringUtils.isNotEmpty(lotInfoEntity.getFRAME_INFO())) {
                    map.addProperty("RLFINFO", lotInfoEntity.getFRAME_INFO());//传框架信息
                }
//                map.addProperty("LOTACTIONLIST",etLot.getText().toString());//传批次动作列表
                NetUtils.requestPostNet("IF.LOTTRACKIN", method, context, map, callBack);

            }
        }, () -> {
        }).show(fragmentManager, "SUBMIT_DIALOG");
    }

    public static void lotBindTool(Activity context, FragmentManager fragmentManager, String toolID, String eqpID, boolean actionType) {
        FreeApi_DialogUtils.getConfirmAndCancelDialog(context, "确定要提交?", new FreeUI_CommonCallback() {
            @Override
            public void onSuccess() {
                FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                        (new TypeToken<BaseResponseBean<JSONObject>>() {
                        }.getType(), fragmentManager, context) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(context, false, fragmentManager)) {
                            FreeApi_ToastUtils.showFreeToast(actionType ? "绑定成功" : "解绑成功", context, true, 0);
                            context.finish();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<JSONObject>> response) {
                        super.onError(response);
                        FreeApi_LogUtils.saveErrorMessages(response.getException(), "TOOLREPORT");
                        FreeApi_ToastUtils.showFreeToast("连接服务器失败", context, false, 0);
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE", actionType ? "UP" : "DOWN");
                map.addProperty("EQUIPMENTID", eqpID);
                List<ToolEntity> toolList = new ArrayList<>();
                ToolEntity toolEntity = new ToolEntity(toolID);
                toolList.add(toolEntity);
                map.add("TOOLLIST", new JsonParser().parse(new Gson().toJson(toolList)).getAsJsonArray());
//                NetUtils.requestPostNet("CPC.TOOLREPORT", "TOOLREPORT", context, map, callBack);
            }
        }, () -> {

        }).show(fragmentManager, "TOOLREPORT");
    }

    public static void searchRecipeInfo(Context context, FragmentManager fragmentManager, String chipThickness,LotInfoEntity lotInfoEntity, UpdateIPEntity updateIPEntity, String eqpInfoEntity, netCallback listener, String processName, String eqpType, String plantName) {
        FreeUI_EntityCallBack<BaseResponseBean<RecipeInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<RecipeInfoEntity>>
                ("自定义loading", new TypeToken<BaseResponseBean<RecipeInfoEntity>>() {
                }.getType(), fragmentManager, context) {

            @Override
            public void onSuccess(Response<BaseResponseBean<RecipeInfoEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(context, false, fragmentManager)) {
                    if (listener != null) {
                        listener.onSuccess(response.body().getResponse().getBody());
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<RecipeInfoEntity>> response) {
                super.onError(response);
                if (listener != null) {
                    listener.onFail(response.getException());
                }
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", eqpInfoEntity);
        map.addProperty("EqpType", updateIPEntity.getPROGRAMTYPE());
        map.addProperty("ProductionInfo", lotInfoEntity.getLOT().getPARTNAME());
        map.addProperty("ProcessName", updateIPEntity.getPROCESSNAME());
        map.addProperty("BD_CODE", lotInfoEntity.getBD_CODE());
        map.addProperty("PACKAGETYPE", lotInfoEntity.getPACKAGETYPE());
        map.addProperty("CHIPNAME", lotInfoEntity.getCHIPNAME());
        map.addProperty("WIRESPEC", lotInfoEntity.getWIRESPEC());
        map.addProperty("WAFERSIZE", lotInfoEntity.getWAFERSIZE());
        map.addProperty("DIESIZE", lotInfoEntity.getDIESIZE());
        map.addProperty("SPC2", lotInfoEntity.getPART().getSPEC2());
        map.addProperty("DieIsSingle",lotInfoEntity.getISSINGLE());
        map.addProperty("CHIPTHICKNESS",chipThickness);
        NetUtils.requestPostNet("IF.GetRecipeAvailable", "GetRecipeAvailable", context, map, callBack);
    }

    public static void recipeDownLoad(Activity context, FragmentManager fragmentManager, RecipeInfoEntity recipeInfoEntity, String eqpID, String lotID, int enableEQ) {
        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                (new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), fragmentManager, context) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(context, false, fragmentManager)) {
                    FreeApi_ToastUtils.showFreeToast("下载成功", context, true, 0);
                    context.finish();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                FreeApi_LogUtils.saveErrorMessages(response.getException(), "SendEqpRecipe");
                FreeApi_ToastUtils.showFreeToast("连接服务器失败", context, false, 0);

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("PlantName", recipeInfoEntity.getPlantName());
        map.addProperty("ProcessName", recipeInfoEntity.getProcessName());
        map.addProperty("EqpID", eqpID);
        map.addProperty("RecipeEqpID", recipeInfoEntity.getRecipeEqpID());
        map.addProperty("RecipeID", recipeInfoEntity.getRecipeID());
        map.addProperty("RecipeVer", recipeInfoEntity.getRecipeVer());
        map.addProperty("LotID", lotID);
        map.addProperty("EnableEQ", enableEQ);
        NetUtils.requestPostNet("CPC.SendEqpRecipe", "SendEqpRecipe", context, map, callBack);
    }

    public static void publicAudit(Context context, FragmentManager fragmentManager, String lotID, String innerBoxID, String outerBoxID, netCallback listener) {
        FreeUI_EntityCallBack<BaseResponseBean<PackageDetailListEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<PackageDetailListEntity>>
                (new TypeToken<BaseResponseBean<PackageDetailListEntity>>() {
                }.getType(), fragmentManager, context) {

            @Override
            public void onSuccess(Response<BaseResponseBean<PackageDetailListEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(context, false, fragmentManager)) {
                    if (listener != null) {
                        PackageDetailListEntity body = response.body().getResponse().getBody();
                        body.setPACKLOTID(innerBoxID);
                        body.setSOURCELOTID(lotID);
                        listener.onSuccess(body);
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<PackageDetailListEntity>> response) {
                super.onError(response);
                if (listener != null) {
                    listener.onFail(response.getException());
                }

            }
        };
        JsonObject map = new JsonObject();
        // L210300005
        // 2109_0000032
        map.addProperty("ACTION_TYPE", "APPROVE");
//        map.addProperty("ACTION_TYPE", "QUERY");
        map.addProperty("LOT_ID", lotID);
        map.addProperty("CHECKPACKLOTID", innerBoxID);
        map.addProperty("PARENTPACKLOTID", outerBoxID);
        map.addProperty("CHECKTYPE", "caseCheck");
        NetUtils.requestPostNet("IF.LBLAPPROVE", "Common", context, map, callBack);

    }

    public static void publicAudit(Activity context, FragmentManager fragmentManager, String actionType, String packagrType, List<PackageDetailListEntity> data) {
        FreeApi_DialogUtils.getConfirmAndCancelDialog(context, "确定要提交?", new FreeUI_CommonCallback() {
            @Override
            public void onSuccess() {
                FreeUI_EntityCallBack<BaseResponseBean<PackageDetailListEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<PackageDetailListEntity>>
                        (new TypeToken<BaseResponseBean<PackageDetailListEntity>>() {
                        }.getType(), fragmentManager, context) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<PackageDetailListEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(context, false, fragmentManager)) {
                            FreeApi_ToastUtils.showFreeToast("核对成功", context, true, 0);
                            context.finish();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<PackageDetailListEntity>> response) {
                        super.onError(response);
                        FreeApi_LogUtils.saveErrorMessages(response.getException(), "PUBLICAUDIT");
                        FreeApi_ToastUtils.showFreeToast("连接服务器失败", context, false, 0);

                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE", actionType);
                map.addProperty("PACKAGETYPE", packagrType);
                map.add("PACKAGEDETAILLIST", new JsonParser().parse(new Gson().toJson(data)).getAsJsonArray());
                NetUtils.requestPostNet("CPC.PUBLICAUDIT", "PUBLICAUDIT", context, map, callBack);
            }
        }, () -> {

        }).show(fragmentManager, "TOOLATTACHEQP");

    }

    public static void packageUpShelf(Activity context, FragmentManager fragmentManager, String lotID, String storageID, boolean packageUp, String consumeQTY, String isEndFlag, netCallback listener) {
        FreeApi_DialogUtils.getConfirmAndCancelDialog(context, "确定要提交?", new FreeUI_CommonCallback() {
            @Override
            public void onSuccess() {
                FreeUI_EntityCallBack<BaseResponseBean<PackageEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<PackageEntity>>
                        (new TypeToken<BaseResponseBean<PackageEntity>>() {
                        }.getType(), fragmentManager, context) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<PackageEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(context, false, fragmentManager)) {
                            if (listener != null) {
                                listener.onSuccess(response.body().getResponse().getBody());
                                return;
                            }
                            FreeApi_ToastUtils.showFreeToast(packageUp ? "上架成功" : "下架成功", context, true, 0);
                            context.finish();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<PackageEntity>> response) {
                        super.onError(response);
                        FreeApi_LogUtils.saveErrorMessages(response.getException(), "PACKAGEUPSHELF");
                        FreeApi_ToastUtils.showFreeToast("连接服务器失败", context, false, 0);

                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE", packageUp ? "BIND" : "UNBIND");
                map.addProperty("LOTID", lotID);
                map.addProperty("STORAGEID", storageID);
                map.addProperty("CONSUMEQTY", consumeQTY);
                map.addProperty("ISENDFLAG", isEndFlag);
                NetUtils.requestPostNet("IF.PACKAGEUPSHELF", "Common", context, map, callBack);
            }
        }, () -> {

        }).show(fragmentManager, "TOOLATTACHEQP");
    }

    public static void lotFTTrackIn(FragmentManager fragmentManager, Activity context, String carrierStrap, String coverStrap,String eqpID, String lotID, List<String> carrierList, String actionType, List<String> eqpTestList, String lineID,LotInfoEntity lotInfoEntity) {
        FreeApi_DialogUtils.getConfirmAndCancelDialog(context, "确定要提交?", new FreeUI_CommonCallback() {
            @Override
            public void onSuccess() {
                //确定

                FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                        (new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                        }.getType(), fragmentManager, context) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(context, false, fragmentManager)) {
                            LotInfoEntity body = response.body().getResponse().getBody();
                            if (body.getIsSampled().equals("Y")) {
                                FreeApi_DialogUtils.getConfirmDialog(context, "确定", lotID + "批次要分" + body.getNUM() + "颗样品！",
                                        () -> context.finish()).show(fragmentManager, "MES_DIALOG");
                            } else {
                                FreeApi_ToastUtils.showFreeToast("进站成功", context, true, 0);
                                context.finish();
                            }
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                        super.onError(response);
                        FreeApi_LogUtils.saveErrorMessages(response.getException(), "LOTTRACKIN");
                        FreeApi_ToastUtils.showFreeToast("连接服务器失败", context, false, 0);
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE", actionType);
                map.addProperty("EQUIPMENTID", eqpID);
                map.addProperty("LINEID", lineID);
                map.addProperty("DOCTYPE",lotInfoEntity.getWORKORDER().getDOCTYPE());
                map.addProperty("ORDER_NO",lotInfoEntity.getLOT().getWOID());
                map.addProperty("PACKAGETYPE", lotInfoEntity.getPACKAGETYPE());
                map.addProperty("PARTNAME", lotInfoEntity.getLOT().getPARTNAME());
                map.addProperty("PARTTYPE", lotInfoEntity.getLOT().getPARTTYPE());
                map.addProperty("WIRESPEC", lotInfoEntity.getWIRESPEC());
                map.addProperty("BDCODE", lotInfoEntity.getBD_CODE());
                map.addProperty("CARRIERTAPE", carrierStrap);// 载带数据
                map.addProperty("COVERSTRAP", coverStrap);// 盖带数据
                map.addProperty("FKNIFE", lotInfoEntity.getFKNIFE());
                map.addProperty("PRODUCTTYPE", lotInfoEntity.getPART().getSPEC2());
                map.addProperty("CUSTOMERCODE", lotInfoEntity.getLOT().getCUSTOMERCODE());
                map.addProperty("CHIPNAME", lotInfoEntity.getCHIPNAME());
                map.addProperty("WOID", lotInfoEntity.getLOT().getWOID());
                map.addProperty("RESERVED3", lotInfoEntity.getWORKORDER().getRESERVED3());
                map.addProperty("SEALAPPROVEDFLAG", lotInfoEntity.getWORKORDER().getUDF().getsealApprovedFlag());
                map.addProperty("ISCHECKSTEPAUTHORITY", false);
                map.addProperty("PARTDESC", lotInfoEntity.getLOT().getPARTDESC());
                map.addProperty("reserved5", lotInfoEntity.getLOT().getUDF().getRESERVED5());
                map.add("DURABLELIST", new JsonParser().parse(new Gson().toJson(carrierList)).getAsJsonArray());
                List<LotList> lotList = new ArrayList<>();
                lotList.add(new LotList(lotID));
                map.add("LOTLIST", new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());//传批次集合
                map.add("SUBEQUIPMENTIDLIST", new JsonParser().parse(new Gson().toJson(eqpTestList)).getAsJsonArray());
                NetUtils.requestPostNet("IF.LOTFTTRACKIN", "LOTFTTRACKIN", context, map, callBack);

            }
        }, () -> {
        }).show(fragmentManager, "SUBMIT_DIALOG");
    }

    public static void binReport(Activity context, FragmentManager fragmentManager, String actionType, String testType, String lotID,
                                 String eqpID, LotInfoEntity lotInfoEntity, List<BinInfoEntity.BinInfo> binList, String gradeNo, String eqpType,
                                 String testEqp, String StationID, String messageName, String state, AvailableProgramBean availableProgramBean, String testblock,String location) {
        FreeApi_DialogUtils.getConfirmAndCancelDialog(context, "确定要提交?", new FreeUI_CommonCallback() {
            @Override
            public void onSuccess() {
                //确定
                FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                        (new TypeToken<BaseResponseBean<JSONObject>>() {
                        }.getType(), fragmentManager, context) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(context, false, fragmentManager)) {
                            FreeApi_ToastUtils.showFreeToast("提交成功", context, true, 0);
                            context.finish();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<JSONObject>> response) {
                        super.onError(response);
                        FreeApi_LogUtils.saveErrorMessages(response.getException(), messageName);
                        FreeApi_ToastUtils.showFreeToast("连接服务器失败", context, false, 0);
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE", actionType);
                map.addProperty("TESTTYPE", testType);
                map.addProperty("LOTID", lotID);
                map.addProperty("PRODUCTMODEL", lotInfoEntity.getPRODUCTMODEL());
                map.addProperty("WIRINGNO", lotInfoEntity.getWIRINGNO());
                map.addProperty("SEALNO", lotInfoEntity.getSEALNO());
                map.addProperty("CHIPNAME", lotInfoEntity.getCHIPNAME());
                map.addProperty("WIRESPEC", lotInfoEntity.getWIRESPEC());
                map.addProperty("LINEID", lotInfoEntity.getLINEID());
                map.addProperty("EQUIPMENTID", eqpID);
                map.addProperty("CHIPLOT", lotInfoEntity.getCHIPID());
                map.addProperty("LOTTYPE", lotInfoEntity.getLOT().getLOTTYPE());
                map.addProperty("PACKAGETYPE", lotInfoEntity.getLOT().getMAINMATTYPE());
                map.addProperty("CUSTOMERCODE", lotInfoEntity.getLOT().getCUSTOMERCODE());
                map.addProperty("LOTCOUNT", lotInfoEntity.getLOT().getMAINQTY());
                map.addProperty("SPC2", lotInfoEntity.getPART().getSPEC2());
                map.addProperty("GRADENO", gradeNo);
                map.addProperty("LOCATION", location);
                map.addProperty("EQPTYPE", eqpType);
                map.addProperty("TESTERIDS", testEqp);
                map.addProperty("StationID", StationID);
                map.addProperty("TESTBLOCK", testblock);
                map.addProperty("GradeNo", availableProgramBean.getGradeno());
                map.addProperty("CHIPNAME", lotInfoEntity.getCHIPNAME());
                map.addProperty("OBJECTRRN", lotInfoEntity.getLOT().getOBJECTRRN());
                map.addProperty("DESCRIPTION", lotInfoEntity.getLOT().getPARTDESC());
                map.addProperty("PARTNAME", lotInfoEntity.getLOT().getPARTNAME());
                map.addProperty("PARTTYPE", lotInfoEntity.getLOT().getPARTTYPE());
                map.addProperty("CONNECTIONSTATE", state);
                map.addProperty("ProductType", lotInfoEntity.getLOT().getMATERIALTYPE());
                map.addProperty("FileUrl",availableProgramBean.getFileUrl());
                map.addProperty("FileRemark", availableProgramBean.getFileRemark());
                map.addProperty("FileName", availableProgramBean.getFileName());
                map.addProperty("SiteNo", availableProgramBean.getSiteNo());
                map.addProperty("ID", availableProgramBean.getID());
                map.addProperty("TestType", availableProgramBean.getTestType());
                map.addProperty("ProgramGear", availableProgramBean.getProgramGear());
                map.addProperty("TOTALLSL", lotInfoEntity.getTOTALLSL());
                map.add("HBINLIST", new JsonParser().parse(new Gson().toJson(binList)).getAsJsonArray());
                NetUtils.requestPostNet(messageName, messageName, context, map, callBack);

            }
        }, () -> {
        }).show(fragmentManager, "SUBMIT_DIALOG");

    }

    public static void lotDAWBTrackOut(Activity context, String method, FragmentManager fragmentManager, String storageid, String eqpWork, String lotID,
                                       List<ScrapEntity> defectData, List<String> carrierList, String defectCode, String subqty, LotInfoEntity lotInfoEntity,
                                       int defectNum, String trackNum, List<EDCListBean.ListBean> edcList, netCallback callback) {
        FreeUI_GeneralFragmentDialog freeU_generalFragmentDialog = new FreeUI_GeneralFragmentDialog();
        View view = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.dialog_trackout, null);
        TextView tvLotID = view.findViewById(R.id.tvLotID);
        tvLotID.setText(lotID);
        TextView tvStep = view.findViewById(R.id.tvStep);
        tvStep.setText(lotInfoEntity.getLOT().getSTEPNAME() + "/" + lotInfoEntity.getLOT().getSTEPDESC());
        TextView tvEqpID = view.findViewById(R.id.tvEqpID);
        tvEqpID.setText(eqpWork);
        TextView tvTrackInQTY = view.findViewById(R.id.tvTrackInQTY);
        tvTrackInQTY.setText(subqty);
        TextView tvTrackOutQTY = view.findViewById(R.id.tvTrackOutQTY);
        int mSubqty = Integer.parseInt(subqty);
        tvTrackOutQTY.setText(trackNum.equals("") ? String.valueOf(mSubqty - defectNum) : trackNum);
        TextView tvDefectNum = view.findViewById(R.id.tvDefectNum);
        TextView tvDefect = view.findViewById(R.id.tvDefect);
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(2);
        int sumDefect = 0;
        if (defectData != null) {
            for (ScrapEntity entity : defectData) {
                if (!entity.getACTIONREASON().contains("不记录良率")) {
                    sumDefect += Integer.parseInt(entity.getMAINQTY().equals("") ? "0" : entity.getMAINQTY());
                }
            }
        } else {
            sumDefect += 0;
        }

        tvDefectNum.setText(String.valueOf(sumDefect));
//        String result = numberFormat.format(((float) (mainQTY - sumDefect) / (float) mainQTY) * 100);
//        String result = numberFormat.format(((float) (Integer.parseInt(tvTrackOutQTY.getText().toString())) / (float) mSubqty) * 100);
//        BigDecimal b1 = new BigDecimal(Double.toString(Long.parseLong(tvTrackOutQTY.getText().toString())));
        BigDecimal b2 = new BigDecimal(Double.toString(mSubqty));
        BigDecimal b1 = new BigDecimal(Double.toString(Integer.parseInt(tvTrackOutQTY.getText().toString())));
        double v1 = b1.divide(b2, 5, BigDecimal.ROUND_HALF_UP).doubleValue();
        double v2 = v1 * 100;
        tvDefect.setText(v2 + "%");

        TextView tvCancel = view.findViewById(R.id.tvCancel);
        TextView tvConfirm = view.findViewById(R.id.tvConfirm);
        tvCancel.setOnClickListener(view1 -> {
            freeU_generalFragmentDialog.dismiss();
        });
        tvConfirm.setOnClickListener(v -> {
            FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                    (new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                    }.getType(), fragmentManager, context) {

                @Override
                public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onSuccess(response);
                    FreeApi_LogUtils.saveErrorMessages(response.toString(),"lotDAWBTrackOut");
                    if (response.body().isSuccess(context, false, fragmentManager)) {
                        String holdstate = response.body().getResponse().getBody().getLOTLIST().get(0).getHOLDSTATE();
                        FreeApi_DialogUtils.getConfirmDialog(context, "确定", holdstate.equals("On") ? "该批次良率超标！" : "出站成功", () -> {
                            if (!method.equals("Common")){
                                context.finish();
                            }else {
                                callback.onSuccess("");
                            }
                        }).show(fragmentManager, "CONFIRM_DIALOG");
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onError(response);
                    FreeApi_LogUtils.saveErrorMessages(response.getException(), "LOTTRACKOUT");
                    FreeApi_ToastUtils.showFreeToast("连接服务器失败", context, false, 0);
                }
            };
            JsonObject map = new JsonObject();
//            String url = "";
//            if (method.equals("Common")){
//                url = "InspectErrorTrackOut";
//            }else if (method.equals("GENERALLOTTRACKOUT")){
//                url = "MultiEqpNoNumTrackIn";
//            }else {
//                url = "DefaultTrackOut";
//            }
//            map.addProperty("ACTIONTYPE", url);
            map.addProperty("ACTIONTYPE", method.equals("Common") ? "InspectErrorTrackOut" : "DefaultTrackOut");
            map.addProperty("EQUIPMENTID", eqpWork);
            if (method.equals("MULTIDEVICELOTTRACKOUT")){
                map.addProperty("TYPE", "1");
            }
            map.addProperty("CARRIERACTION", "");
            map.addProperty("DURABLE", "");
            map.addProperty("ALLREWORKFLAG", "N");
            map.addProperty("WAREHOUSEID", defectCode);
            map.addProperty("STORAGEID", storageid);
            map.addProperty("SPC2", lotInfoEntity.getPART().getSPEC2());
            List<LotLists> lotList = new ArrayList<>();
            List<LotactionLists> lotactionList = new ArrayList<>();
            LotLists lotList1 = new LotLists(lotID,lotInfoEntity.getLOT().getMAINQTY());
            lotList.add(lotList1);

            if (defectData != null) {
                for (ScrapEntity mDefectData : defectData) {
                    ArrayList<LotactionLists.QTYList> qtyList = new ArrayList<>();
                    LotactionLists.QTYList qtybean = new LotactionLists.QTYList();

                    qtybean.setSUBQTY("0");
                    qtybean.setMAINQTY(mDefectData.getMAINQTY());
                    qtyList.add(qtybean);

//                    LotactionLists lotactionLists = new LotactionLists(lotID,
//                            mDefectData.getACTIONCODE(),
//                            mDefectData.getACTIONREASON(),
//                            method.equals("Common") ? mDefectData.getMAINQTY() : "0",
//                            method.equals("Common") ? "0" : mDefectData.getMAINQTY(),
//                            qtyList);

                    LotactionLists lotactionLists1 = new LotactionLists();
                    lotactionLists1.setLOTID(lotID);
                    lotactionLists1.setACTIONCODE(mDefectData.getACTIONCODE());
                    lotactionLists1.setACTIONREASON(mDefectData.getACTIONREASON());
                    lotactionLists1.setACTIONTYPE("DSLOTTRACKOUT".equals(method)?"Scrap":"Defect");
                    lotactionLists1.setSUBQTY(mDefectData.getSUBQTY());
                    lotactionLists1.setMAINQTY(mDefectData.getMAINQTY());
                    lotactionLists1.setQTYLIST(qtyList);

                    lotactionList.add(lotactionLists1);

                }
            }

            map.add("LOTLIST", new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());//传批次集合
            map.add("LOTACTIONLIST", new JsonParser().parse(new Gson().toJson(lotactionList)).getAsJsonArray());
            if (edcList != null){
                map.add("EDCLIST", new JsonParser().parse(new Gson().toJson(edcList)).getAsJsonArray());
            }
            NetUtils.requestPostNet("IF.LOTTRACKOUT", method, context, map, callBack);
            freeU_generalFragmentDialog.dismiss();
        });

        freeU_generalFragmentDialog.setLayout(view);
        freeU_generalFragmentDialog.show(fragmentManager, "SUBMIT_DIALOG");
    }

    public interface netCallback<T> {
        void onSuccess(T response);

        void onFail(Throwable t);
    }

    public interface TrackOutCallback<T> {
        void onFail(Throwable t);

        void onCancel();
    }

}
